<template>
  <div id="register">
    <h2>会员注册</h2>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item class="redItem" label="请输入邮箱" prop="email">
        <el-input v-model="ruleForm.email">
          <el-button @click="Code" :loading="ruleForm.numberState" style="padding-right:10px" slot="suffix" type="text" >获取验证码</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="请输入验证码" prop="code">
        <el-input v-model="ruleForm.code" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="请输入车牌号" prop="number">
        <el-input v-model="ruleForm.number"></el-input>
      </el-form-item>
      <el-form-item label="请输入昵称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <div id="button">
        <el-form-item>
          <el-button type="primary" @click="submitForm">注册</el-button>
          <el-button @click="resetForm('ruleForm')">清空</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login_register",
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'));
      }
    };
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('昵称不能为空'));
      }
    };
    var checkNumber = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('车牌号不能为空'));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入密码'));
      }
    };
    var code = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入密码'));
      }
    };
    return {
      ruleForm: {
        pass: '',
        email: '',
        number: '',
        name: '',
        code: '',
        checkCode: '',
        numberState:false,
      },
      rules: {
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        email: [
          {validator: checkAge, trigger: 'blur'}
        ],
        number: [
          {validator: checkNumber, trigger: 'blur'}
        ],
        name: [
          {validator: checkName, trigger: 'blur'}
        ],
        code: [
          {validator: code, trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    Code() {
      if (this.ruleForm.email == '') {
        this.$message({
          message: '请先输入邮箱再获取验证码！',
          type: 'warning'
        });
      } else {
        this.ruleForm.numberState=true;
        const timer = setInterval(() => {
          this.ruleForm.numberState=false;//你所加载数据的方法
        }, 10000)
        this.$once('hook:beforeDestroy', () => {
          clearInterval(timer)
        })
        this.$http.get("http://localhost:8088/user/code", {
          params: {
            email: this.ruleForm.email
          }
        }).then(resp => {
          this.ruleForm.checkCode = resp.data;
          console.log(resp.data)
        })
      }
    },
    submitForm() {
      console.log("login")
      if (this.ruleForm.pass == '' || this.ruleForm.email == '' || this.ruleForm.number == '' || this.ruleForm.name == '' || this.ruleForm.code == '') {
        this.$message({
          message: '请输入完整！',
          type: 'warning'
        });
      } else if (this.ruleForm.code != this.ruleForm.checkCode) {
        this.$message.error('验证码输入错误！请重新输入！');
      } else {
        this.$http.get("http://localhost:8088/user/add", {
          params: {
            pass: this.ruleForm.pass,
            email: this.ruleForm.email,
            number: this.ruleForm.number,
            name: this.ruleForm.name,
          }
        }).then(resp => {
          if (resp.data === 1) {
            this.login_user();
          } else if (resp.data === 0) {
            alert("账号重复,请重新输入!")
          }
        }).catch({})
      }

    },
    login_user() {
      this.$message({
        message: '恭喜你，注册成功！',
        type: 'success'
      });
      this.$router.push({
        path: `/login/login_user`,
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style scoped>
#register {
  border: 1px solid white;
  padding-left: 0;
  padding-right: 60px;
  padding-top: 0;
  margin: auto;
  width: 500px;
  padding-bottom: 5px;
  border-radius: 10px;
  height: 500px;

}

h2{
  color:#000;
  padding-left: 70px;
}
#button{
  padding-top: 10px;
}
</style>